<?php
	
	/**
	 * Verifies bootstrapper execution to ensure file
	 * necessities, and disabling direct access
	 */
	defined('ENGINE') or exit;
	
	/**
	 * IP Address validation, check if supplied
	 * ipv4 or ipv6 address is written in correct
	 * syntax
	 */
	function is_valid_ip($address = NULL)
	{
		$address = (string) $address;
		
		if(!is_null($address))
		{
			if(extension_loaded('filter'))
			{
				return((boolean) filter_var($address, FILTER_VALIDATE_IP));
			}
			else
			{
				if((is_valid_ipv4($address)) || (is_valid_ipv6($address)))
				{
					return(true);
				}
			}
		}
		
		return(false);
	}
	
	/**
	 * IPv4 validation, check if supplied
	 * ipv4 address is written in correct syntax
	 */
	function is_valid_ipv4($address = NULL)
	{
		$address = (string) $address;
		
		if(!is_null($address))
		{
			if(preg_match('/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $address))
			{
				foreach(explode('.', $address) as $part)
				{
					if(!(intval($part) > 255 or intval($part) < 0))
					{
						return(true);
					}
				}
			}
		}
		
		return(false);
	}
	
	/**
	 * IPv6 validation, check if supplied
	 * ipv6 address is written in correct syntax
	 */
	function is_valid_ipv6($address = NULL)
	{
		$address = (string) $address;
		
		if(!is_null($address))
		{
			if(preg_match('/^(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4})*|[a-f0-9]{1,4}(?::[a-f0-9]{1,4})*::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4})*)?|::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4})*)?) (?::\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/ix', $address))
			{
				return(true);
			}
		}
		
		return(false);
	}

?>